package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.NideshopCollect;
import com.kgc.scd.bean.ShopGoods;
import com.kgc.scd.mapper.CollectMapper;
import com.kgc.scd.mapper.ShopCoodsMapper;
import com.kgc.scd.service.CollectService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.SnowUtil;
import com.kgc.scd.vo.NideshopCollectVO;
import com.kgc.scd.vo.ShopGoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : yantao
 * <p>
 * Description: CollectServiceImpl
 */
@Slf4j
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private SnowUtil snowUtil;

    @Autowired
    private ShopCoodsMapper shopCoodsMapper;

    @Autowired
    private   CollectMapper collectMapper;

    /**
     * @author : yantao
     * @date   : 2022/11/14
     * @param  : [pageNo, pageSize]
     * @return : java.util.List<com.kgc.scd.vo.NideshopCollectVO>
     * @description : 查询所有信息
     */
    @Override
    public PageSupport<NideshopCollectVO> selectCollectAll(Integer pageNo, Integer pageSize,String name,String userId) {

        log.info("###### server端selectCollectAll调用:{},{} ######",pageNo,pageSize);
        PageSupport<NideshopCollectVO> pageSupport = new PageSupport<>();

        IPage<NideshopCollect> userPage = new Page<>(pageNo, pageSize);

        //生成mybatis-plus,增加条件weapper
        QueryWrapper<NideshopCollect> queryWrapper = new QueryWrapper<>();

        //条件用户id
        queryWrapper.eq("user_id",userId);


        queryWrapper.orderByDesc("add_time");
        //条件查询带分页,通过userId查询信息
        userPage = collectMapper.selectPage(userPage,queryWrapper);
        List<NideshopCollect> list = userPage.getRecords();
        log.info("###### 查询数据: {} ######",list);
        //返回list类型vo模型
        List<NideshopCollectVO> nideshopCollectVOS = new ArrayList<>();

        //进行VO模型的映射
        list.forEach(a ->{
            NideshopCollectVO nideshopCollectVO = new NideshopCollectVO();

            QueryWrapper<ShopGoods> shopGoodsQueryWrapper = new QueryWrapper<>();
            ShopGoodsVO shopGoodsVO = new ShopGoodsVO();
            BeanUtils.copyProperties(a,nideshopCollectVO);
            nideshopCollectVO.setIsAttention(a.getIsAttention()==1 ? "以预购":"未预购");
            //shopGoodsQueryWrapper.like(true,"name",name);
            shopGoodsQueryWrapper.eq("id",a.getGoodsId());
            //通过商品id查询商品信息,goodsId
            ShopGoods shopGoods = shopCoodsMapper.selectOne(shopGoodsQueryWrapper);

            BeanUtils.copyProperties(shopGoods,shopGoodsVO);
            //映射是否已经售空
            shopGoodsVO.setIsSoldout(shopGoods.getGoodsNumber()>0 ? "未售空":"以售空");
            nideshopCollectVO.setShopGoodsVO(shopGoodsVO);

            nideshopCollectVOS.add(nideshopCollectVO);

        });
        log.info("###### ytserver查询的数据:{} ######",nideshopCollectVOS);

        //分页参数进行加载,加载到pageSupport类中进行
        pageSupport.setPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalPage((int) userPage.getPages());
        pageSupport.setTotalCount((int) userPage.getTotal());
        pageSupport.setPageData(nideshopCollectVOS);

        log.info("###### 返回数据:{} ######",pageSupport);
        return pageSupport;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId, goodsId]
     * @return : boolean
     * @description : 查询收藏情况,进行收藏时
     */
    @Override
    public boolean selectIsCollect(String userId, String goodsId) {

        Map<String,String> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("goods_id",goodsId);

        QueryWrapper<NideshopCollect> queryWrapper = new QueryWrapper<>();

        queryWrapper.allEq(map);


        return !ObjectUtils.isEmpty(collectMapper.selectOne(queryWrapper));
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId, goodsId]
     * @return : boolean
     * @description : 删除收藏byuserId
     */
    @Override
    public boolean delectCollectByUserId(String userId, String goodsId) {

        log.info("###### 开始删除:{},{} ######",userId,goodsId);

        Map<String,String> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("goods_id",goodsId);

        QueryWrapper<NideshopCollect> queryWrapper = new QueryWrapper<>();

        queryWrapper.allEq(map);

        return collectMapper.delete(queryWrapper)>0 ? true:false;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [userId, goodsId]
     * @return : boolean
     * @description : 添加收藏
     */
    @Override
    public boolean insertCollect(String userId, String goodsId) {
        log.info("###### 开始添加:{},{} ######",userId,goodsId);
        NideshopCollect nideshopCollect = new NideshopCollect(snowUtil.snowId(),userId,goodsId,0 ,new  Date());


        return collectMapper.insert(nideshopCollect)>0;
    }


}
